package org.bluweb.g4wicket.search.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import org.bluweb.g4wicket.G4WicketException;

import com.google.gson.Gson;


public class SearchService {

	public SearchResponse search( SearchRequest request ) {

		//TODO encode parameter
		
		try {
			URL url = new URL( "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + request.getQuery() );
			BufferedReader reader = new BufferedReader( new InputStreamReader( url.openStream() ) );
			return new Gson().fromJson( reader, SearchResponse.class );
		} catch (Exception e) {
			throw new G4WicketException( e );
		}
	}
}
